package sg.nus.iss.vmcs.dao;

import sg.nus.iss.vmcs.VMCSException;
import sg.nus.iss.vmcs.models.DrinksBrand;
import sg.nus.iss.vmcs.models.DrinksStoreItem;
import sg.nus.iss.vmcs.models.AbstractStoreFactory;
import sg.nus.iss.vmcs.utils.StoreType;

/**
 * Property Loader for Drinks Store
 * @author Pang Pingli
 */
public class DrinkPropertyLoader extends PropertyLoader {

	/**
	 * Creates an Instance of the Drink Property Loader
	 * @param filen File Name to load from
	 */
	public DrinkPropertyLoader(String filen) {
    	super(filen);
	}

	/**
	 * Retrieves a Drink Store Item and use factory to create
	 * @param idx Index of the item to retrieve
	 * @return Instance of a DrinkStoreItem
	 */
	public DrinksStoreItem getItem(int idx){
		//Use factory to create store objects and items.
		AbstractStoreFactory dsFactory = AbstractStoreFactory.getStoreFactory(StoreType.DRINKS);
	    DrinksBrand db = (DrinksBrand) dsFactory.makeStoreObject();
	    String item = new String("Name" + idx);
	    String itemv = getValue(item);
	
	    db.setName(itemv);
	
	    item = new String("Price" + idx);
	    itemv = getValue(item);
	    db.setPrice(Integer.parseInt(itemv));
	
	    item = new String("Quantity" + idx);
	    itemv = getValue(item);
	
		try {
			DrinksStoreItem cit = (DrinksStoreItem) dsFactory.makeStoreItem(db, Integer.parseInt(itemv));
		    return cit;
		} 
		catch (NumberFormatException e) {
			e.printStackTrace();
		} 
		catch (VMCSException e) {
			e.printStackTrace();
		}
		
		return null;
	}

	/**
	 * Sets a Drink Store Item
	 * @param it Index of Drink Store Item to set
	 * @param item Drink Store Item to set
	 */
	public void setItem(int it, DrinksStoreItem item) {
	    int idx = it + 1;
	    DrinksBrand cn = (DrinksBrand) item.getContent();
	    String itn = new String("Name" + idx);
	
	    setValue(itn, cn.getName());
	
	    itn = new String("Price" + idx);
	    setValue(itn, String.valueOf(cn.getPrice()));
	
	    itn = new String("Quantity" + idx);
	    setValue(itn, String.valueOf(item.getQuantity()));
	}
}
